package com.school.hjt.schoolclasscard.model;



import android.content.Context;
import android.util.Log;

import com.school.hjt.schoolclasscard.BaseApplication;
import com.school.hjt.schoolclasscard.db.Album;
import com.school.hjt.schoolclasscard.db.CardDao;
import com.school.hjt.schoolclasscard.db.CardStudent;
import com.school.hjt.schoolclasscard.db.ClassAlbum;
import com.school.hjt.schoolclasscard.db.ClassInfo;
import com.school.hjt.schoolclasscard.db.DynamicSchool;
import com.school.hjt.schoolclasscard.db.Labours;
import com.school.hjt.schoolclasscard.db.SchArticle;
import com.school.hjt.schoolclasscard.db.SchInfo;
import com.school.hjt.schoolclasscard.db.StudentSign;
import com.school.hjt.schoolclasscard.utlis.APPUtlis;
import com.school.hjt.schoolclasscard.utlis.ClassCalendar;
import com.school.hjt.schoolclasscard.utlis.Constant;
import com.school.hjt.schoolclasscard.utlis.PrefUtild;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xqx on 2018/1/10.
 *首页model
 */

public class HomeFModel {
    private int num=0;
    public String getMotto(){
        BaseApplication.getDaoInstant().clear();
        List<SchInfo> schInfos = CardDao.querySchInfo();
        if (schInfos.size()!=0){
            return schInfos.get(0).getMotto().trim().replace("\n","");
        }
        return "暂无";
    }
    public String getSchoolImg(){
        BaseApplication.getDaoInstant().clear();
        List<SchInfo> schInfos = CardDao.querySchInfo();
        if (schInfos.size()!=0){
            return schInfos.get(0).getSchool_img();
        }
        return "";
    }
    public List<String> getAlbum(){
        BaseApplication.getDaoInstant().clear();
        List<String> list=new ArrayList<>();
        list.clear();
        List<ClassAlbum> classAlba = CardDao.queryAlbumType("1");
        if (classAlba.size()>9){
            for (int i=0;i<8;i++){
                list.add(classAlba.get(i).getPic());
            }
        }else{
            for (ClassAlbum alba:classAlba){
                list.add(alba.getPic());
            }
        }
        return list;
    }
    public List<String> getClassAlbum(){
        BaseApplication.getDaoInstant().clear();
        List<String> list=new ArrayList<>();
        list.clear();
        List<Album> alba1 = CardDao.queryClassAlbum();
        if (alba1.size()>9){
            for (int i=0;i<8;i++){
                list.add(alba1.get(i).getPic_path());
            }
        }else{
            for (Album alba:alba1){
                list.add(alba.getPic_path());
            }
        }
        return list;
    }
    public  List<SchArticle> getArticle(){
        BaseApplication.getDaoInstant().clear();
        List<SchArticle> schArticles = CardDao.querySchArticle();
        if (schArticles.size()>=5){
            return schArticles.subList(0,5);
        }
//        if (schArticles.size()!=0){
//            if (num<schArticles.size()){
////                Log.e("ATG",num+" A");
//                SchArticle str=schArticles.get(num);
//                num++;
//                return str;
//            }else if (num>=schArticles.size()){
//                num=0;
//                SchArticle str=schArticles.get(num);
//                num++;
//                return str;
//            }
//        }else {
//            return new SchArticle();
//        }
        return schArticles;
    }
    //校园动态
    public List<String> ArtSchools(){
        BaseApplication.getDaoInstant().clear();
        List<String> strclass=new ArrayList<>();
        List<DynamicSchool> dynamics = CardDao.queryDynamicSchool();
        if (dynamics.size()!=0) {
            for (DynamicSchool dynamic : dynamics) {
                String str=dynamic.getContent().trim().replace("\n","");
                strclass = APPUtlis.handleText(str,20,19.5f);
            }
        }else{
            strclass.add("");
        }
        return strclass;
    }
    public Labours getLabours(){
        BaseApplication.getDaoInstant().clear();
        List<Labours> labourses = CardDao.queryLabours();
        if (labourses.size()==0){
            return new Labours();
        }
        return labourses.get(0);
    }
    public String getClassMotto(){
        BaseApplication.getDaoInstant().clear();
        List<ClassInfo> classInfos = CardDao.queryClassInfo();
        if (classInfos.size()!=0){
           return classInfos.get(0).getClass_motto().trim().replace('\n',' ');
        }
        return "";
    }
    public List<String> getLabTeacher(){
        BaseApplication.getDaoInstant().clear();
        List<Labours> labourses = CardDao.queryLabours();
        List<String> strclass=new ArrayList<>();
        strclass.clear();
        int num=0;
        int num1;
        StringBuilder builder=new StringBuilder();
        if (labourses.size()!=0){
            String lab_teacher = labourses.get(0).getLab_teacher();
            if (!lab_teacher.equals("")){
                String[] all=lab_teacher.split(" ");
                num1=all.length%3;
                for (int i=0;i<all.length;i++){
                    ++num;
                    if(num!=3){
                        builder.append(all[i]);
                        builder.append(" ");
                    }else{
                        builder.append(all[i]);
                        strclass.add(builder.toString());
                        builder.setLength(0);
                        num=0;
                    }
                    if (num1!=0&&i==all.length-1){
                        strclass.add(builder.toString());
                    }
                }
            }
        } else {
            strclass.add("");
        }
       return strclass;
    }
    public List<String> getAdmTeacher(){
        BaseApplication.getDaoInstant().clear();
        List<Labours> labourses = CardDao.queryLabours();
        List<String> strclass=new ArrayList<>();
        strclass.clear();
        int num=0;
        int num1;
        StringBuilder builder=new StringBuilder();
        if (labourses.size()!=0){
            String lab_teacher = labourses.get(0).getAdm_teacher();
            if (!lab_teacher.equals("")){
                String[] all=lab_teacher.split(" ");
                num1=all.length%3;
                for (int i=0;i<all.length;i++){
                    ++num;
                    if(num!=3){
                        builder.append(all[i]);
                        builder.append(" ");
                    }else{
                        builder.append(all[i]);
                        strclass.add(builder.toString());
                        builder.setLength(0);
                        num=0;
                    }
                    if (num1!=0&&i==all.length-1){
                        strclass.add(builder.toString());
                    }
                }
            }
        }else {
            strclass.add("");
        }
        return strclass;
    }
    public int getNum(){
        BaseApplication.getDaoInstant().clear();
        List<CardStudent> cardStudents = CardDao.queryStudent();
        return cardStudents.size();
    }
    public String getTo(Context contxt, int type){
        BaseApplication.getDaoInstant().clear();
        String num = PrefUtild.INSTANCE.getString(contxt, Constant.NUM, "0");
        if (type==1){
            return num;
        }else{
            List<CardStudent> cardStudents = CardDao.queryStudent();
            int noTo=cardStudents.size()-Integer.parseInt(num);
            return noTo+"";
        }
    }
    public int getTo(){
        BaseApplication.getDaoInstant().clear();
        int coun=0;
            List<StudentSign> studentSigns = CardDao.querySign();
            for (StudentSign sign : studentSigns) {
                long time = Long.parseLong(sign.getTime());
                String stringDays = ClassCalendar.getStringDays("06", "00");
                if (time > Long.parseLong(stringDays)) {
                    coun++;
                }
            }
        return coun;
    }
    public List<CardStudent> getFace(){
        BaseApplication.getDaoInstant().clear();
        List<CardStudent> students1 = CardDao.queryStudent();
        List<CardStudent> students=new ArrayList<>();
        students.clear();
        for (CardStudent student:students1){
            StudentSign studentSign = student.getStudentSign();
            if (studentSign!=null){
                long time = Long.parseLong(studentSign.getTime());
                String stringDays = ClassCalendar.getStringDays("06", "00");
                if (time > Long.parseLong(stringDays)&&studentSign.getSigntype()==0) {
                    students.add(student);
                }
            }
        }
        return students;
    }
    //返回实到人数
    public List<CardStudent> getTonum(){
        BaseApplication.getDaoInstant().clear();
        List<CardStudent> students1 = CardDao.queryStudent();
        List<CardStudent> students=new ArrayList<>();
        students.clear();
            for (CardStudent student:students1){
                StudentSign studentSign = student.getStudentSign();
                if (studentSign!=null){
                        long time = Long.parseLong(studentSign.getTime());
                        String stringDays = ClassCalendar.getStringDays("06", "00");
                        if (time > Long.parseLong(stringDays)) {
                            students.add(student);
                    }
                }
            }
        return students;
    }
    public List<CardStudent> getonTonum(){
        List<CardStudent> cardStudents = CardDao.queryStudent();
        List<CardStudent> students=new ArrayList<>();
        students.clear();
            for (CardStudent student:cardStudents){
                StudentSign studentSign = student.getStudentSign();
                if (studentSign!=null){
                        long time = Long.parseLong(studentSign.getTime());
                        String stringDays = ClassCalendar.getStringDays("06", "00");
                        if (time < Long.parseLong(stringDays)) {
                            students.add(student);
                        }
                }else {
                    students.add(student);

                }
            }
        return students;
    }
}
